package com.zhong.gang.pda.ui.login;

import android.content.Intent;
import android.view.View;

import com.zhong.gang.applib.utils.Md5Utils;
import com.zhong.gang.applib.http.BaseDataBean;
import com.zhong.gang.applib.http.OkHttpUtils;
import com.zhong.gang.applib.http.callback.DataCallBack;
import com.zhong.gang.pda.api.Api;
import com.zhong.gang.pda.db.user.UserBean;
import com.zhong.gang.pda.db.user.UserUtils;
import com.zhong.gang.pda.ui.main.MainActivity;

public class LoginPresenter {

    public final void login(String phone, String pass, View errPassView,LoginAct act){
        OkHttpUtils.post().url(Api.login)
                .addParams("username",phone)
                .addParams("password", Md5Utils.getMd5(pass))
                .build().execute(errPassView.getContext(),"登录",new DataCallBack<BaseDataBean<UserBean>>(act){
            @Override
            public void onResponse(BaseDataBean<UserBean> data) throws Exception {
                super.onResponse(data);
                if (data.getCode() == 200){
                    UserUtils.getIntent(act).saveUser(data.getResult());
                    act.startActivity(new Intent(act, MainActivity.class));
                    act.closeActivity();
                    return;
                }
                errPassView.setVisibility(View.VISIBLE);
            }
        });
    }
}
